﻿// ===============================================================================
// Project Name        :    Gfw.Repositories.Stock
// Project Description :    
// ===============================================================================
// Class Name          :    BargainPoolRepository
// Namespace:          :    Gfw.Repositories.Stock
// Class Version       :    v1.0.0.0
// Class Description   :    
// CLR Version:        :    4.0.30319.17626
//Author               :    lif
// Machine             :    ADICO-PC
// Create Time         :    2012/7/14 15:46:40
// Update Time         :    2012/7/14 15:46:40
// ===============================================================================
// Copyright © lif 2012 . All rights reserved.
// ===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gfw.Models.Stock;
using Gfw.Repository;

namespace Gfw.Repositories.Stock
{
    public class WaitBargainPoolRepository : Repository<StkWaitbargainpool>
    {
        private static object lockhelper = new object();

        public virtual StkWaitbargainpool Unique(UnitOfWork work = null)
        {
            return GetAll(work).First();
        }

        public override IEnumerable<StkWaitbargainpool> GetAll(UnitOfWork unitOfWork = null)
        {
            if (unitOfWork == null)
            {
                var criteria = Session.CreateCriteria<StkWaitbargainpool>().AddOrder(new NHibernate.Criterion.Order("AgencyPrice", true)).AddOrder(new NHibernate.Criterion.Order("AgencyDate", true)).SetCacheMode(NHibernate.CacheMode.Ignore);
                return criteria.List<StkWaitbargainpool>();

            }
            else
            {
                var criteria =
                    unitOfWork.Session.CreateCriteria<StkWaitbargainpool>().AddOrder(new NHibernate.Criterion.Order("AgencyPrice", true)).AddOrder(new NHibernate.Criterion.Order("AgencyDate", true)).SetCacheMode(NHibernate.CacheMode.Ignore);
                return criteria.List<StkWaitbargainpool>();
            }
        }


        

        public override void Update(StkWaitbargainpool entity, UnitOfWork unitOfWork = null)
        {
            lock (lockhelper)
            {
                base.Update(entity, unitOfWork);
            }
        }


        public IList<StkWaitbargainpool> GetTopSell5()
        {
            var hql = "from StkWaitbargainpool where TradeType=0 order by AgencyPrice ,AgencyDate";
            var query = Session.CreateQuery(hql).SetMaxResults(5);
            return query.List<StkWaitbargainpool>();
        }
        public IList<StkWaitbargainpool> GetTopBuy5()
        {
            var hql = "from StkWaitbargainpool where TradeType=1 order by AgencyPrice desc ,AgencyDate";
            var query = Session.CreateQuery(hql).SetMaxResults(5);
            return query.List<StkWaitbargainpool>();
        }
    }
}
